// ///<reference path="vector4.ts" />
// ///<reference path="../util/pool.ts" />

//找不到归属的放这里

namespace mathD 
{
    //row：图片行数//column:图片列数//index：第几张图片（index从0开始计数）
    export function spriteAnimation(row: number, column: number, index: number, out: Vector4) {
        var width = 1 / column;
        var height = 1 / row;
        var offsetx = width * (index % column);
        var offsety = height * Math.floor(index / column);

        out.x = width;
        out.y = height;
        out.z = offsetx;
        out.w = offsety;
        // var uvOffset=new gd3d.math.vector4(width,height,offsetx,offsety);
        // return  uvOffset;
    }

    export function GetPointAlongCurve(curveStart:Vector3,curveStartHandle:Vector3,curveEnd:Vector3,curveEndHandle:Vector3,t:number, out:Vector3,crease:number=0.3)
    {
        var oneMinT = 1 - t;
        var oneMinTPow3 =Math.pow(oneMinT, 3);
        var oneMinTPow2 = Math.pow(oneMinT, 2);

        var oneMinCrease = 1 - crease;

        var tempt1=mathD.pool.new_vector3();
        Vector3.scaleByNum(curveStart,oneMinTPow3*oneMinCrease,tempt1);
        var tempt2=mathD.pool.new_vector3();
        Vector3.scaleByNum(curveStartHandle,3*oneMinTPow2*t*crease,tempt2);
        var tempt3=mathD.pool.new_vector3();
        Vector3.scaleByNum(curveEndHandle,3*oneMinT*Math.pow(t,2)*crease,tempt3);
        var tempt4=mathD.pool.new_vector3();
        Vector3.scaleByNum(curveEnd,Math.pow(t,3)*oneMinCrease,tempt4);

        var tempt5=mathD.pool.new_vector3();
        Vector3.add(tempt1,tempt2,tempt5);
        Vector3.add(tempt5,tempt3,tempt5);
        Vector3.add(tempt5,tempt4,tempt5);
        
        Vector3.scaleByNum(tempt5,1/(oneMinTPow3*oneMinCrease+3*oneMinTPow2*t*crease+3*oneMinT*Math.pow(t,2)*crease+Math.pow(t,3)*oneMinCrease),out);

        mathD.pool.delete_vector3(tempt1);
        mathD.pool.delete_vector3(tempt2);
        mathD.pool.delete_vector3(tempt3);
        mathD.pool.delete_vector3(tempt4);
        mathD.pool.delete_vector3(tempt5);
    }
    export function floatClamp(v: number, min: number = 0, max: number = 1): number
    {
        if (v < min)
            return min;
        else if (v > max)
            return max;
        else
            return v;
    }
    export function sign(value: number): number
    {
        value = +value; // convert to a number

        if (value === 0 || isNaN(value))
            return value;

        return value > 0 ? 1 : -1;
    }

    export function getKeyCodeByAscii(ev: KeyboardEvent)
    {
        if (ev.shiftKey)
        {
            return ev.keyCode - 32;
        } else
        {
            return ev.keyCode;
        }
    }


    export function numberLerp(fromV: number, toV: number, v: number)
    {
        return fromV * (1 - v) + toV * v;
    }
}